<?php

class db_modules extends dbModel
{
	protected $sSource = "modules";
	protected $sId_coll = "module_code";

	//get modules (by student_code, class_code or both)
	function listModules($student_code = false, $class_code = false)
	{
		
		//if there is no student code given
		if(!$student_code && $class_code) {
			
			//sql query
			$sSql = "SELECT 
						m.module_code, m.module_name 
					FROM 
						modules m, connect_module_class cmc 
					WHERE 
						cmc.class_code LIKE '$class_code%' 
					AND 
						m.module_code = cmc.module_code
					AND 
						m.is_active = '1'
					AND
						m.is_archived = '0'";
			
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
		
		//if there is no class code given
		} else if(!$class_code && $student_code) {
			
			//sql query
			$sSql = "SELECT 
						m.module_code, m.module_name 
					FROM 
						modules m, connect_module_class cmc, connect_student_class csc
					WHERE 
						csc.student_code LIKE '$student_code%' 
					AND 
						cmc.class_code = csc.class_code
					AND 
						m.module_code = cmc.module_code
					AND
						m.is_active = '1'
					AND
						m.is_archived = '0'";
			
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
		
		//if both are given
		} else if($class_code && $student_code){
		
			//sql query
			$sSql = "SELECT 
						m.module_code, m.module_name 
					FROM 
						modules m, connect_module_class cmc, connect_student_class csc
					WHERE 
						csc.student_code LIKE '$student_code%'
					AND 
						cmc.class_code LIKE '$class_code%'
					AND 
						cmc.class_code = csc.class_code
					AND
						m.module_code = cmc.module_code
					AND
						m.is_active = '1'
					AND
						m.is_archived = '0'";
			
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
			
		}
		else
		{
			//$this->aCols('module_name', 'module_code');
		}
	}
	
	//get functie
	function getModules($sCode = false, $sName = false, $bIs_active = false, $bIs_deleted = false)
	{
		
		if($sCode) $this->search($sCode, 'module_code');
		if($sName) $this->search($sName, 'module_name');
		if($bIs_active) $this->search($bIs_active, 'is_active');
		if($bIs_deleted) $this->search($bIs_deleted, 'is_deleted');
	
	}

	public function fetch_modules()
	{
		$this->has_many();
	}

	public function search_module($sInput)
	{
		$this->search($sInput, Array("module_code", "module_name"));
	}

	//het systeem
	function archiveModule($sCode)
	{
	$this->trashcan();
	$this[$sCode] = Array("is_archived" => 1);
	$this->save();
	}
 
 	//de gebruiker
	function setActiveModule($sCode, $bActive)
	{
	$this->trashcan();
	$this[$sCode] = Array("is_active" => $bActive);
	$this->save();
	}

}
?> 

